clc 
close all
clear all
%%PRKS
pdr95_pro_rd1 = GetStableRegionDelay('light-prks-pdr95-delay-timeslot-rd1.txt');
pdr95_pro_rd2 = GetStableRegionDelay('light-prks-pdr95-delay-timeslot-rd2.txt');
pdr95_pro_rd3 = GetStableRegionDelay('light-prks-pdr95-delay-timeslot-rd3.txt');
pdr95_pro_rd4 = GetStableRegionDelay('light-prks-pdr95-delay-timeslot-rd4.txt');
pdr95_pro_rd5 = GetStableRegionDelay('light-prks-pdr95-delay-timeslot-rd5.txt');
delay_pro_95 = [pdr95_pro_rd1; pdr95_pro_rd2;pdr95_pro_rd3;pdr95_pro_rd4;pdr95_pro_rd5];

pdr90_pro_rd1 = GetStableRegionDelay('light-prks-pdr90-delay-timeslot-rd1.txt');
pdr90_pro_rd2 = GetStableRegionDelay('light-prks-pdr90-delay-timeslot-rd2.txt');
pdr90_pro_rd3 = GetStableRegionDelay('light-prks-pdr90-delay-timeslot-rd3.txt');
pdr90_pro_rd4 = GetStableRegionDelay('light-prks-pdr90-delay-timeslot-rd4.txt');
pdr90_pro_rd5 = GetStableRegionDelay('light-prks-pdr90-delay-timeslot-rd5.txt');
delay_pro_90 = [pdr90_pro_rd1; pdr90_pro_rd2;pdr90_pro_rd3;pdr90_pro_rd4;pdr90_pro_rd5];

pdr80_pro_rd1 = GetStableRegionDelay('light-prks-pdr80-delay-timeslot-rd1.txt');
pdr80_pro_rd2 = GetStableRegionDelay('light-prks-pdr80-delay-timeslot-rd2.txt');
pdr80_pro_rd3 = GetStableRegionDelay('light-prks-pdr80-delay-timeslot-rd3.txt');
pdr80_pro_rd4 = GetStableRegionDelay('light-prks-pdr80-delay-timeslot-rd4.txt');
pdr80_pro_rd5 = GetStableRegionDelay('light-prks-pdr80-delay-timeslot-rd5.txt');
delay_pro_80 = [pdr80_pro_rd1; pdr80_pro_rd2;pdr80_pro_rd3;pdr80_pro_rd4;pdr80_pro_rd5];

pdr70_pro_rd1 = GetStableRegionDelay('light-prks-pdr70-delay-timeslot-rd1.txt');
pdr70_pro_rd2 = GetStableRegionDelay('light-prks-pdr70-delay-timeslot-rd2.txt');
pdr70_pro_rd3 = GetStableRegionDelay('light-prks-pdr70-delay-timeslot-rd3.txt');
pdr70_pro_rd4 = GetStableRegionDelay('light-prks-pdr70-delay-timeslot-rd4.txt');
pdr70_pro_rd5 = GetStableRegionDelay('light-prks-pdr70-delay-timeslot-rd5.txt');
delay_pro_70 = [pdr70_pro_rd1; pdr70_pro_rd2;pdr70_pro_rd3;pdr70_pro_rd4;pdr70_pro_rd5];

%%RID
pdr95_rid_rd1 = GetStableRegionDelay('light-rid-pdr95-delay-timeslot-rd1.txt');
pdr95_rid_rd2 = GetStableRegionDelay('light-rid-pdr95-delay-timeslot-rd2.txt');
pdr95_rid_rd3 = GetStableRegionDelay('light-rid-pdr95-delay-timeslot-rd3.txt');
pdr95_rid_rd4 = GetStableRegionDelay('light-rid-pdr95-delay-timeslot-rd4.txt');
pdr95_rid_rd5 = GetStableRegionDelay('light-rid-pdr95-delay-timeslot-rd5.txt');
delay_rid_95 = [pdr95_rid_rd1; pdr95_rid_rd2;pdr95_rid_rd3;pdr95_rid_rd4;pdr95_rid_rd5];

pdr90_rid_rd1 = GetStableRegionDelay('light-rid-pdr90-delay-timeslot-rd1.txt');
pdr90_rid_rd2 = GetStableRegionDelay('light-rid-pdr90-delay-timeslot-rd2.txt');
pdr90_rid_rd3 = GetStableRegionDelay('light-rid-pdr90-delay-timeslot-rd3.txt');
pdr90_rid_rd4 = GetStableRegionDelay('light-rid-pdr90-delay-timeslot-rd4.txt');
pdr90_rid_rd5 = GetStableRegionDelay('light-rid-pdr90-delay-timeslot-rd5.txt');
delay_rid_90 = [pdr90_rid_rd1; pdr90_rid_rd2;pdr90_rid_rd3;pdr90_rid_rd4;pdr90_rid_rd5];

pdr80_rid_rd1 = GetStableRegionDelay('light-rid-pdr80-delay-timeslot-rd1.txt');
pdr80_rid_rd2 = GetStableRegionDelay('light-rid-pdr80-delay-timeslot-rd2.txt');
pdr80_rid_rd3 = GetStableRegionDelay('light-rid-pdr80-delay-timeslot-rd3.txt');
pdr80_rid_rd4 = GetStableRegionDelay('light-rid-pdr80-delay-timeslot-rd4.txt');
pdr80_rid_rd5 = GetStableRegionDelay('light-rid-pdr80-delay-timeslot-rd5.txt');
delay_rid_80 = [pdr80_rid_rd1; pdr80_rid_rd2;pdr80_rid_rd3;pdr80_rid_rd4;pdr80_rid_rd5];

pdr70_rid_rd1 = GetStableRegionDelay('light-rid-pdr70-delay-timeslot-rd1.txt');
pdr70_rid_rd2 = GetStableRegionDelay('light-rid-pdr70-delay-timeslot-rd2.txt');
pdr70_rid_rd3 = GetStableRegionDelay('light-rid-pdr70-delay-timeslot-rd3.txt');
pdr70_rid_rd4 = GetStableRegionDelay('light-rid-pdr70-delay-timeslot-rd4.txt');
pdr70_rid_rd5 = GetStableRegionDelay('light-rid-pdr70-delay-timeslot-rd5.txt');
delay_rid_70 = [pdr70_rid_rd1; pdr70_rid_rd2;pdr70_rid_rd3;pdr70_rid_rd4;pdr70_rid_rd5];

%%B-MAC
csma_delay_rd1 = GetStableRegionDelay('light-csma-delay-timeslot-rd1.txt');
csma_delay_rd2 = GetStableRegionDelay('light-csma-delay-timeslot-rd2.txt');
csma_delay_rd3 = GetStableRegionDelay('light-csma-delay-timeslot-rd3.txt');
csma_delay_rd4 = GetStableRegionDelay('light-csma-delay-timeslot-rd4.txt');
csma_delay_rd5 = GetStableRegionDelay('light-csma-delay-timeslot-rd5.txt');
csma_delay = [csma_delay_rd1; csma_delay_rd2;csma_delay_rd3;csma_delay_rd4;csma_delay_rd5];

%%S-MAC
rtscts_delay_rd1 = GetStableRegionDelay('light-rtscts-delay-timeslot-rd1.txt');
rtscts_delay_rd2 = GetStableRegionDelay('light-rtscts-delay-timeslot-rd2.txt');
rtscts_delay_rd3 = GetStableRegionDelay('light-rtscts-delay-timeslot-rd3.txt');
rtscts_delay_rd4 = GetStableRegionDelay('light-rtscts-delay-timeslot-rd4.txt');
rtscts_delay_rd5 = GetStableRegionDelay('light-rtscts-delay-timeslot-rd5.txt');
rtscts_delay = [rtscts_delay_rd1; rtscts_delay_rd2;rtscts_delay_rd3;rtscts_delay_rd4;rtscts_delay_rd5];

ALPHA=0.05;

MEAN = zeros(4,4);
MEAN(1,1) = mean(delay_pro_70(:,3));
MEAN(1,2) = mean(csma_delay(:,3));
MEAN(1,3) = mean(rtscts_delay(:,3));
MEAN(1,4) = mean(delay_rid_70(:,3));
Error_MEAN(1,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_70(:,3)) / sqrt(size(delay_pro_70(:,3), 1));
Error_MEAN(1,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEAN(1,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEAN(1,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_70(:,3)) / sqrt(size(delay_rid_70(:,3), 1));
MEAN(2,1) = mean(delay_pro_80(:,3));
MEAN(2,2) = mean(csma_delay(:,3));
MEAN(2,3) = mean(rtscts_delay(:,3));
MEAN(2,4) = mean(delay_rid_80(:,3));
Error_MEAN(2,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_80(:,3)) / sqrt(size(delay_pro_80(:,3), 1));
Error_MEAN(2,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEAN(2,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEAN(2,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_80(:,3)) / sqrt(size(delay_rid_80(:,3), 1));
MEAN(3,1) = mean(delay_pro_90(:,3));
MEAN(3,2) = mean(csma_delay(:,3));
MEAN(3,3) = mean(rtscts_delay(:,3));
MEAN(3,4) = mean(delay_rid_90(:,3));
Error_MEAN(3,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_90(:,3)) / sqrt(size(delay_pro_90(:,3), 1));
Error_MEAN(3,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEAN(3,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEAN(3,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_90(:,3)) / sqrt(size(delay_rid_90(:,3), 1));
MEAN(4,1) = mean(delay_pro_95(:,3));
MEAN(4,2) = mean(csma_delay(:,3));
MEAN(4,3) = mean(rtscts_delay(:,3));
MEAN(4,4) = mean(delay_rid_95(:,3));
Error_MEAN(4,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_95(:,3)) / sqrt(size(delay_pro_95(:,3), 1));
Error_MEAN(4,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEAN(4,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEAN(4,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_95(:,3)) / sqrt(size(delay_rid_95(:,3), 1));

figure;
handle = barweb(MEAN, Error_MEAN,1, ['70%';'80%';'90%';'95%'], [], ['PDR requirement'], ['Mean Delay'], [1 1 1; 0.7 0.7 0.7; 0.2 0.2 0.2; 0 0 0]);
handle.legend =legend ('PRKS','B-MAC','S-MAC','RIDB','orientation','horizontal');
set(gca,'fontsize',60);
box on
maximize ();
saveas(gcf, 'delay_across_different_protocols-light.jpg');
saveas(gcf, 'delay_across_different_protocols-light.eps');
saveas(gcf, 'delay_across_different_protocols-light.fig');


%{
MEDIAN = zeros(4,4);
MEDIAN(1,1) = median(delay_pro_70(:,3));
MEDIAN(1,2) = median(csma_delay(:,3));
MEDIAN(1,3) = median(rtscts_delay(:,3));
MEDIAN(1,4) = median(delay_rid_70(:,3));
Error_MEDIAN(1,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_70(:,3)) / sqrt(size(delay_pro_70(:,3), 1));
Error_MEDIAN(1,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(1,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(1,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_70(:,3)) / sqrt(size(delay_rid_70(:,3), 1));
MEDIAN(2,1) = median(delay_pro_80(:,3));
MEDIAN(2,2) = median(csma_delay(:,3));
MEDIAN(2,3) = median(rtscts_delay(:,3));
MEDIAN(2,4) = median(delay_rid_80(:,3));
Error_MEDIAN(2,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_80(:,3)) / sqrt(size(delay_pro_80(:,3), 1));
Error_MEDIAN(2,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(2,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(2,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_80(:,3)) / sqrt(size(delay_rid_80(:,3), 1));
MEDIAN(3,1) = median(delay_pro_90(:,3));
MEDIAN(3,2) = median(csma_delay(:,3));
MEDIAN(3,3) = median(rtscts_delay(:,3));
MEDIAN(3,4) = median(delay_rid_90(:,3));
Error_MEDIAN(3,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_90(:,3)) / sqrt(size(delay_pro_90(:,3), 1));
Error_MEDIAN(3,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(3,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(3,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_90(:,3)) / sqrt(size(delay_rid_90(:,3), 1));
MEDIAN(4,1) = median(delay_pro_95(:,3));
MEDIAN(4,2) = median(csma_delay(:,3));
MEDIAN(4,3) = median(rtscts_delay(:,3));
MEDIAN(4,4) = median(delay_rid_95(:,3));
Error_MEDIAN(4,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_95(:,3)) / sqrt(size(delay_pro_95(:,3), 1));
Error_MEDIAN(4,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(4,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(4,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_95(:,3)) / sqrt(size(delay_rid_95(:,3), 1));

figure;
handle = barweb(MEDIAN, Error_MEDIAN,1, ['70%';'80%';'90%';'95%'], [], ['PDR requirement'], ['Median Delay'], [1 1 1; 0.7 0.7 0.7; 0.2 0.2 0.2; 0 0 0]);
handle.legend =legend ('PRKS','B-MAC','S-MAC','RIDB','orientation','horizontal');
set(gca,'fontsize',60);
box on
maximize ();
saveas(gcf, 'median_delay_across_different_protocols.jpg');
saveas(gcf, 'median_delay_across_different_protocols.eps');
saveas(gcf, 'median_delay_across_different_protocols.fig');
%}
